package com.mf.listener;

import com.alibaba.fastjson2.JSON;
import com.mf.config.RabbitMqConfig;
import com.mf.service.impl.WebSocketServiceImpl;
import io.github.heollhai.common.entity.request.SocketMessageRequest;
import io.github.heollhai.common.utils.VerifyUtils;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @Author: lmf
 * @Create: 2024/11/26 21:33
 * Description:  mq消息监听
 */
@Component
public class MqListener {

    @RabbitListener(
            bindings = @org.springframework.amqp.rabbit.annotation.QueueBinding(
                    value = @Queue(RabbitMqConfig.QUEUE_NAME),
                    exchange = @org.springframework.amqp.rabbit.annotation.Exchange(value = RabbitMqConfig.EXCHANGE_NAME, type = "topic"),
                    key = RabbitMqConfig.COMPLETE_TASK_KEY
            ))
    public void onMessage(String message) {
        System.out.println("接收到消息>>>>>>>>>>>>：1" + message);
        final SocketMessageRequest messageRequest = JSON.parseObject(message, SocketMessageRequest.class);
        if (VerifyUtils.isEmpty(messageRequest.getUserId())) {
            WebSocketServiceImpl.sendAllMessage(messageRequest);
        } else {
            WebSocketServiceImpl.sendTextMessageByUserId(messageRequest);
        }
    }

//     @RabbitListener(
//            bindings = @org.springframework.amqp.rabbit.annotation.QueueBinding(
//                    value = @Queue(RabbitMqConfig.QUEUE_NAME),
//                    exchange = @org.springframework.amqp.rabbit.annotation.Exchange(value = RabbitMqConfig.EXCHANGE_NAME, type = "topic"),
//                    key = RabbitMqConfig.COMPLETE_TASK_KEY2
//            ))
//    public void onMessage2(String message) {
//        System.out.println("接收到消息>>>>>>>>>>>>：2" + message);
//        final SocketMessageRequest messageRequest = JSON.parseObject(message, SocketMessageRequest.class);
//        if (VerifyUtils.isEmpty(messageRequest.getUserId())) {
//            WebSocketServiceImpl.sendAllMessage(messageRequest);
//        } else {
//            WebSocketServiceImpl.sendTextMessageByUserId(messageRequest);
//        }
//    }
}
